package com.lzy.webapi.facade;

import com.lzy.base.apis.DeptServiceApi;
import com.lzy.base.dtos.DeptDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.product.apis.OrderProductServiceApi;
import com.lzy.product.apis.ProductUserServiceApi;
import com.lzy.product.dtos.OrderProductDto;
import com.lzy.product.dtos.ProductUserDto;
import com.lzy.webapi.response.ProductUserResponse;
import com.lzy.webapi.response.TeamInfoResponse;
import com.lzy.webapi.response.TeamResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProductUserFacade {

    @Autowired
    ProductUserServiceApi productUserServiceApi;
    @Autowired
    DeptServiceApi deptServiceApi;
    @Autowired
    OrderProductServiceApi orderProductServiceApi;

    public ProductUserResponse getProfile(Long userId) {
        ProductUserDto userDto = productUserServiceApi.findById(userId);
        Ensure.that(userDto).isNotNull("01000005");
        ProductUserResponse userResponse = new ProductUserResponse();
        userResponse.setTeamId(userDto.getId());
        userResponse.setAvatar(userDto.getAvatar());
        userResponse.setJobId(userDto.getJobNumber());
        userResponse.setJoinTime(userDto.getCreateTime());
        userResponse.setName(userDto.getRealName());
        userResponse.setPhone(userDto.getPhone());
        userResponse.setSex(userDto.getSex());
        DeptDto deptDto = deptServiceApi.findDeptDtoById(userDto.getDeptId());
        if (deptDto != null) {
            userResponse.setDepartment(deptDto.getDeptName());
        }
        return userResponse;
    }

    public TeamResponse getTeam(Long userId) {
        TeamResponse teamResponse = new TeamResponse();
        ProductUserDto userDto = productUserServiceApi.findById(userId);
        Ensure.that(userDto).isNotNull("01000005");
        if (userDto.getUserId() != null) {
            ProductUserResponse leader = new ProductUserResponse();
            ProductUserDto leaderUser = productUserServiceApi.findById(userDto.getUserId());
            if (leaderUser != null) {
                leader.setTeamId(leaderUser.getId());
                leader.setAvatar(leaderUser.getAvatar());
                leader.setJobId(leaderUser.getJobNumber());
                leader.setJoinTime(leaderUser.getCreateTime());
                leader.setName(leaderUser.getRealName());
                leader.setPhone(leaderUser.getPhone());
                leader.setSex(leaderUser.getSex());
                DeptDto deptDto = deptServiceApi.findDeptDtoById(userDto.getDeptId());
                if (deptDto != null) {
                    leader.setDepartment(deptDto.getDeptName());
                }
                teamResponse.setLeader(leader);
            }
            List<ProductUserResponse> member = getLowerUserList(userDto);
            teamResponse.setMember(member);
        }
        return teamResponse;
    }

    public TeamInfoResponse getTeamInfo(Long userId) {
        ProductUserDto userDto = productUserServiceApi.findById(userId);
        Ensure.that(userDto).isNotNull("01000005");
        TeamInfoResponse response = new TeamInfoResponse();
        response.setTeamId(userDto.getId());
        response.setAvatar(userDto.getAvatar());
        response.setJobId(userDto.getJobNumber());
        response.setJoinTime(userDto.getCreateTime());
        response.setName(userDto.getRealName());
        response.setPhone(userDto.getPhone());
        response.setSex(userDto.getSex());
        DeptDto deptDto = deptServiceApi.findDeptDtoById(userDto.getDeptId());
        if (deptDto != null) {
            response.setDepartment(deptDto.getDeptName());
        }
        OrderProductDto orderDto = new OrderProductDto();
        orderDto.setStatus(2);
        orderDto.setCreateUser(userId);
        List<OrderProductDto> list = orderProductServiceApi.selectList(null, orderDto).getList();
        response.setTotalOrder(list.size());
        List<ProductUserResponse> member = getLowerUserList(userDto);
        response.setMember(member);
        return response;
    }

    public List<ProductUserResponse> getLowerUserList(ProductUserDto userDto) {
        List<ProductUserDto> list = productUserServiceApi.selectUserListByUserId(userDto.getId());
        List<ProductUserResponse> member = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (ProductUserDto productUserDto : list) {
                ProductUserResponse response = new ProductUserResponse();
                response.setTeamId(productUserDto.getId());
                response.setAvatar(productUserDto.getAvatar());
                response.setJobId(productUserDto.getJobNumber());
                response.setJoinTime(productUserDto.getCreateTime());
                response.setName(productUserDto.getRealName());
                response.setPhone(productUserDto.getPhone());
                response.setSex(productUserDto.getSex());
                DeptDto dept = deptServiceApi.findDeptDtoById(productUserDto.getDeptId());
                if (dept != null) {
                    response.setDepartment(dept.getDeptName());
                }
                member.add(response);
            }
        }
        return member;
    }
}
